#These add-on software packages are freely available to all R users. Simply use the call function install.packages("tidyverse", "gt", "stringer")
library(tidyverse) 
library(gt)
library(stringer)


FctWhen = function(...) { 
  args = as.list(match.call())
  levels = sapply(args[-1], function(f) f[[3]])
  levels = levels[!is.na(levels)]
  factor(dplyr::case_when(...), levels = levels)
}

df_pre = read_csv('Pre-IPSI Survey 2022_Anon.csv', na = '..')
df_post = read_csv('Post-IPSI Survey 2022_Anon.csv', na = '..')

df_pre = df_pre [-c(1, 2),] 
df_post = df_post[-c(1,2),] 


##Limit datasets to just the variables we're using
df_pre_short = df_pre %>%
  select(Q8_1, Q8_2, Q8_3, Q9_1, Q9_2, Q9_3, Q10_1, Q10_2, Q10_3)

df_post_short = df_post %>%
  select(Q2_1, Q2_2, Q2_3, Q3_1, Q3_2, Q3_3, Q4_1, Q4_2, Q4_3)

## Reshaping
df_pre_long = df_pre_short %>%
  select(1:9) %>%
  pivot_longer(
    cols = 1:9, 
    names_to = 'Question', 
    values_to = 'Response' 
  )

df_post_long = df_post_short %>%
  select(1:9) %>%
  pivot_longer(
    cols = 1:9, 
    names_to = 'Question', 
    values_to = 'Response' 
  )

df_joined = bind_rows(df_pre_long, df_post_long, .id = 'source')

## Split by question
df_joined_knowledgeable = df_joined %>%
  filter(
    Question == "Q8_1" | Question == "Q8_2" | Question == "Q8_3" | Question == "Q2_1" | Question == "Q2_2" |Question == "Q2_3"
  )

df_joined_prepared = df_joined %>%
  filter(
    Question == "Q9_1" | Question == "Q9_2" | Question == "Q9_3" | Question == "Q3_1" | Question == "Q3_2" |Question == "Q3_3"
  )

df_joined_confident = df_joined %>%
  filter(
    Question == "Q10_1" | Question == "Q10_2" | Question == "Q10_3" | Question == "Q4_1" | Question == "Q4_2" |Question == "Q4_3"
  )


## Replace survey answers with numbers
df_knowledgeable_num = df_joined_knowledgeable %>%
  mutate(
    Response = case_when(
      Response == "Very knowledgeable" ~ 5,
      Response == "Knowledgeable" ~ 4,
      Response == "Somewhat knowledgeable" ~ 3,
      Response == "A little knowledgeable" ~ 2,
      Response == "Not at all knowledgeable" ~ 1
    )
  )

df_prepared_num = df_joined_prepared %>%
  mutate(
    Response = case_when(
      Response == "Very well prepared" ~ 5,
      Response == "Well prepared" ~ 4,
      Response == "Somewhat prepared" ~ 3,
      Response == "A little prepared" ~ 2,
      Response == "Not at all prepared" ~ 1
    )
  )

df_confident_num = df_joined_confident %>%
  mutate(
    Response = case_when(
      Response == "Very confident" ~ 5,
      Response == "Confident" ~ 4,
      Response == "Somewhat confident" ~ 3,
      Response == "A little confident" ~ 2,
      Response == "Not at all confident" ~ 1
    )
  )

## Means
df_knowledgeable_num %>%
  group_by(Question) %>%
  summarise_at(
    vars(Response),
    list(name = mean)
  ) 

df_prepared_num %>%
  group_by(Question) %>%
  summarise_at(
    vars(Response),
    list(name = mean)
  ) 

df_confident_num %>%
  group_by(Question) %>%
  summarise_at(
    vars(Response),
    list(name = mean)
  ) 

## Group Means/T-test
#Knowledgeable
gm_knowledgeable = df_knowledgeable_num %>%
  group_by(source) %>%
  summarize(
    Response = mean(Response)
  ) 

gt::gt(gm_knowledgeable)

df_knowledgeable_num %>%
  t.test(Response ~ source, data = .)

#Prepared
gm_prepared = df_prepared_num %>%
  group_by(source) %>%
  summarize(
    Response = mean(Response)
  ) 

gt::gt(gm_prepared)

df_prepared_num %>%
  t.test(Response ~ source, data = .)

#Confident
gm_confident = df_confident_num %>%
  group_by(source) %>%
  summarize(
    Response = mean(Response)
  ) 

gt::gt(gm_confident)

df_confident_num %>%
  t.test(Response ~ source, data = .)

## Reshaping to plot
#Knowledgeable
df_knowledgeable_fct = df_knowledgeable_num %>%
  mutate(
    Response_fct = FctWhen(
      Response == 4 | Response == 5 ~ "Knowledgeable",
      Response %in% 1:3 ~ "Not Knowledgeable"))

df_knowledgeable_sum = df_knowledgeable_fct %>%
  group_by(source, Question) %>%
  count(Response_fct) %>%
  mutate(p = as.integer(n/sum(n) * 100))

df_knowledgeable_top = df_knowledgeable_sum %>%
  filter(Response_fct == "Knowledgeable")

Q8_1_row = list(source = "1", Question = "Q8_1", Response_fct = "Knowledgeable", n = 0, p = 0)
df_knowledgeable_top = rbind (df_knowledgeable_top, Q8_1_row) #adding Q8_1 back in with a zero value so it appears in the plots

df_knowledgeable_final = df_knowledgeable_top %>%
  mutate(
    Question_fct = FctWhen(
      Question == "Q8_1" | Question == "Q2_1" ~ "Conducting media interviews",
      Question == "Q8_2" | Question == "Q2_2" ~ "Writing for non-academic outlets",
      Question == "Q8_3" | Question == "Q2_3" ~ "Engaging with policy officials and practitioners"
    )
  )

#Prepared
df_prepared_fct = df_prepared_num %>%
  mutate(
    Response_fct = FctWhen(
      Response == 4 | Response == 5 ~ "Prepared",
      Response %in% 1:3 ~ "Not Prepared"))

df_prepared_sum = df_prepared_fct %>%
  group_by(source, Question) %>%
  count(Response_fct) %>%
  mutate(p = as.integer(n/sum(n) * 100))

df_prepared_top = df_prepared_sum %>%
  filter(Response_fct == "Prepared")

Q9_1_row = list(source = "1", Question = "Q9_1", Response_fct = "Prepared", n = 0, p = 0)
df_prepared_top = rbind (df_prepared_top, Q9_1_row) #adding Q9_1 back in with a zero value so it appears in the plots

df_prepared_final = df_prepared_top %>%
  mutate(
    Question_fct = FctWhen(
      Question == "Q9_1" | Question == "Q3_1" ~ "Conducting media interviews",
      Question == "Q9_2" | Question == "Q3_2" ~ "Writing for non-academic outlets",
      Question == "Q9_3" | Question == "Q3_3" ~ "Engaging with policy officials and practitioners"
    )
  )

#Confident
df_confident_fct = df_confident_num %>%
  mutate(
    Response_fct = FctWhen(
      Response == 4 | Response == 5 ~ "Confident",
      Response %in% 1:3 ~ "Not Confident"))

df_confident_sum = df_confident_fct %>%
  group_by(source, Question) %>%
  count(Response_fct) %>%
  mutate(p = as.integer(n/sum(n) * 100))

df_confident_top = df_confident_sum %>%
  filter(Response_fct == "Confident")

Q10_1_row = list(source = "1", Question = "Q10_1", Response_fct = "Confident", n = 0, p = 0)
df_confident_top = rbind (df_confident_top, Q10_1_row) #adding Q10_1 back in with a zero value so it appears in the plots

df_confident_final = df_confident_top %>%
  mutate(
    Question_fct = FctWhen(
      Question == "Q10_1" | Question == "Q4_1" ~ "Conducting media interviews",
      Question == "Q10_2" | Question == "Q4_2" ~ "Writing for non-academic outlets",
      Question == "Q10_3" | Question == "Q4_3" ~ "Engaging with policy officials and practitioners"
    )
  )

##Plot
ggsave <- function(..., bg = 'white') ggplot2::ggsave(..., bg = bg)

#Knowledgeable
p1 = df_knowledgeable_final %>%
  ggplot(aes(x = Question_fct, y = p, fill = source)) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(
    y = "Survey Responses (%)", 
    x = NULL, 
    title = NULL) +
  scale_y_continuous(limits = c(0,100), 
                     breaks = seq(0,100,25)) +
  scale_fill_manual(
    name = "Surveys",
    labels = c("Pre-Workshop Survey", "Post-Workshop Survey"),
    values = c("gray1", "gray69")
  ) +
  theme_minimal(base_size = 14) 

p1_fin = p1 +                               
  scale_x_discrete(labels = function(x) str_wrap(x, width = 30))

ggsave(
  plot = p1_fin, 
  file = 'knowledgeable.png',
  height = 6, width = 10, 
  dpi = 1000)

#Prepared
p2 = df_prepared_final %>%
  ggplot(aes(x = Question_fct, y = p, fill = source)) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(
    y = "Survey Responses (%)", 
    x = NULL, 
    title = NULL) +
  scale_y_continuous(limits = c(0,100), 
                     breaks = seq(0,100,25)) +
  scale_fill_manual(
    name = "Surveys",
    labels = c("Pre-Workshop Survey", "Post-Workshop Survey"),
    values = c("gray1", "gray69")
  ) +
  theme_minimal(base_size = 14) 

p2_fin = p2 +                               
  scale_x_discrete(labels = function(x) str_wrap(x, width = 30)) 

ggsave(
  plot = p2_fin, 
  file = 'prepared.png',
  height = 6, width = 10, 
  dpi = 1000 
)

#Confident
p3 = df_confident_final %>%
  ggplot(aes(x = Question_fct, y = p, fill = source)) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(
    y = "Survey Responses (%)", 
    x = NULL, 
    title = NULL) +
  scale_y_continuous(limits = c(0,100), 
                     breaks = seq(0,100,25)) +
  scale_fill_manual(
    name = "Surveys",
    labels = c("Pre-Workshop Survey", "Post-Workshop Survey"),
    values = c("gray1", "gray69")
  ) +
  theme_minimal(base_size = 14) 

p3_fin = p3 +                               
  scale_x_discrete(labels = function(x) str_wrap(x, width = 30))

ggsave(
  plot = p3_fin, 
  file = 'confident.png',
  height = 6, width = 10, 
  dpi = 1000)
